﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Marmat.Forms
{
    public class SplitContainerRenderer
    {
        public SplitContainerRenderer(SplitContainer splitContainer)
        {
            this.SplitContainer = splitContainer;
        }

        protected SplitContainer SplitContainer { get; private set; }

        /// <summary>
    /// 绘制分割器的背景。
    /// </summary>
    /// <param name="e"></param>
        public virtual void OnRenderSplitterBackground(PaintEventArgs e)
        {
            bool bHorizontal = this.SplitContainer.Orientation == Orientation.Horizontal;
            var gradientMode = bHorizontal ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal;
            using (var brush = new LinearGradientBrush(e.ClipRectangle, this.SplitContainer.SplitterBorderExternalColor, this.SplitContainer.SplitterBorderInteriorColor, gradientMode))
            {
                var blend = new Blend();
                blend.Positions = new float[] { 0.0f, 0.5f, 1.0f };
                blend.Factors = new float[] { 0.5f, 1.0f, 0.5f };
                brush.Blend = blend;
                e.Graphics.FillRectangle(brush, e.ClipRectangle);
            }
        }

        /// <summary>
    /// 绘制分割器的命令效果
    /// </summary>
    /// <param name="e"></param>
        public virtual void OnRenderSplitterModalBackground(PaintEventArgs e)
        {
            var arrowRect = default(Rectangle);
            var topLeftRect = default(Rectangle);
            var bottomRightRect = default(Rectangle);
            bool bHorizontal = this.SplitContainer.Orientation == Orientation.Horizontal;
            this.SplitContainer.CalculateRect(e.ClipRectangle, ref arrowRect, ref topLeftRect, ref bottomRightRect);
            var direction = ArrowDirection.Left;
            switch (this.SplitContainer.CollapsePanel)
            {
                case CollapsePanel.Panel1:
                    {
                        if (bHorizontal)
                        {
                            direction = this.SplitContainer.SpliterPanelState == SpliterPanelState.Collapsed ? ArrowDirection.Down : ArrowDirection.Up;
                        }
                        else
                        {
                            direction = this.SplitContainer.SpliterPanelState == SpliterPanelState.Collapsed ? ArrowDirection.Right : ArrowDirection.Left;
                        }

                        break;
                    }

                case CollapsePanel.Panel2:
                    {
                        if (bHorizontal)
                        {
                            direction = this.SplitContainer.SpliterPanelState == SpliterPanelState.Collapsed ? ArrowDirection.Up : ArrowDirection.Down;
                        }
                        else
                        {
                            direction = this.SplitContainer.SpliterPanelState == SpliterPanelState.Collapsed ? ArrowDirection.Left : ArrowDirection.Right;
                        }

                        break;
                    }
            }
            // Dim foreColor As Color = If(_mouseState = ControlState.Hover, Color.FromArgb(21, 66, 139), Color.FromArgb(80, 136, 228))
            // Dim foreColor As Color = If(Me.SplitContainer.MouseState = ControlState.Hover, Color.FromArgb(220, 191, 219, 255), Color.FromArgb(80, 136, 228))
            var foreColor = this.SplitContainer.SplitterBorderForeColor;
            using (var sg = new SmoothingModeGraphics(e.Graphics))
            {
                RenderHelper.RenderGrid(e.Graphics, topLeftRect, new Size(3, 3), foreColor);
                RenderHelper.RenderGrid(e.Graphics, bottomRightRect, new Size(3, 3), foreColor);
                using (Brush brush = new SolidBrush(foreColor))
                {
                    RenderHelper.RenderArrowInternal(e.Graphics, arrowRect, direction, brush);
                }
            }
        }
    }
}